#ifndef _VC10EA_

#include "VideoCompression.h"

#include "gvar.h"

#include "../..//Logrus/Codec.h"
#include "../../Logrus/BitLine.h"
#include "../../Logrus/Sort.h"
#include "../../Win32/yuv_as_rgb.h"


#include <stdio.h>

// ----------------------------- global variables
BYTE			*CMemory=0;	
double  BPS = (double)1024*1024/8/30;
//------------------------------ global extern 
extern int capW, capH;
extern int	QUALITY;
extern double	LOGRUS_reqbitrate; //default bitrate 1 Mbps

#else //ndef _VC10EA_

BYTE			*CMemory;
double  BPS;

#endif //ndef _VC10EA_

BYTE			YBuffer[720*480],
				UVBuffer[720*480];
BYTE			compressed[0x200000];
int	size_compress;

int FPScontrol;
int FPSsent;
unsigned int FPSlasttime;
#define FPSsentMIN -200
#define FPSsentMAX  800


BOOL VideoCompressionCallback(BYTE *pFrame)
{
unsigned int FPSnowtime=0;
unsigned int FPSdelta;
int donotsend; donotsend=0;

	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	QUALITY=20;

#if defined (_J707_) // merge from VC-XR2:#103R210001
	if(bmpFormat == BI_YUY2		 //Added by KAZUHY - VCX103R2
		|| bmpFormat == BI_YUYV	 //Added by KAZUHY - VCX103R2
		|| bmpFormat == BI_YUNV	 //Added by KAZUHY - VCX103R2
		|| bmpFormat == BI_V422) //Added by KAZUHY - VCX103R2
	{
		INTEL_YUY2ToYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0); // Added by KAZUHY - VCX103R2
	} 
	else if(bmpFormat == BI_YVYU) // Added by KAZUHY - VCX103R2
	{
		INTEL_YVYUToYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0); // Added by KAZUHY - VCX103R2
	}
	else if(bmpFormat == BI_UYVY
		 || bmpFormat == BI_UYNV) // Added by KAZUHY - VCX103R2
	{
		INTEL_UYVYToYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW*2, 0, capW);
	}
	else if(bmpFormat == BI_I420 
		 || bmpFormat == BI_IYUV) // Added by KAZUHY - VCX103R2
	{
		INTEL_I420oYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0);
	}
  #ifdef _J707_
	else if (bmpFormat == BI_YV12) //#J7070024
	{
		INTEL_YV12ToYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0);
	}
  #endif
	else
	{
		INTEL_RGBToYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0);
	}
#else
	if(bmpFormat ==  BI_UYVY)
	{
		INTEL_UYVYToYUV422(pFrame,YBuffer,UVBuffer,capW,capH,capW*2,0,capW);
	}
	else if(bmpFormat ==  BI_I420)
	{
		INTEL_I420oYUV422(pFrame, YBuffer, UVBuffer, capW, capH, capW, 0);
	}
	else
	{
		INTEL_RGBToYUV422(pFrame,YBuffer,UVBuffer,capW,capH,capW,0);
	}
#endif
	//size_compress=COMPRESS_FRAME(YBuffer, UVBuffer, UVBuffer+capW*capH/2, capW,capH,
	//		                     compressed, LOGRUS_reqbitrate*BPS, LOGRUS_reqbitrate, 1);

	size_compress=COMPRESS_FRAME(YBuffer, UVBuffer, UVBuffer+capW*capH/2, capW,capH,  compressed, LOGRUS_reqbitrate*BPS/*-size_audio*/, 1, CMemory);


if(FPScontrol!=0)  // check actual FPS
{
  FPSnowtime = timeGetTime();
  FPSdelta = FPSnowtime-FPSlasttime; if ((FPSdelta<0)||(FPSdelta>10000)) FPSdelta=0;
  FPSlasttime = FPSnowtime;

  FPSsent -= FPSdelta; // time(ms)

  if (FPSsent<FPSsentMIN) FPSsent = FPSsentMIN;
  if (FPSsent>FPSsentMAX)
    donotsend = 1; // cannot send, too often
  else
    FPSsent+=1000/FPScontrol; // 1000(ms/s) / speed(frame/s)
}

	if ((cap_process!=NULL) & (donotsend==0))
	{
#ifndef _VC10EA_
		cap_process(compressed,size_compress, TRUE);
#else //ndef _VC10EA_
		Process(compressed, size_compress, TRUE);
#endif //ndef _VC10EA_
	}
	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);

    return (LRESULT) TRUE; 
}


void VideoCompression_SetStillImage(void* RGB,int Mbps)
{
	
	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	INTEL_RGBToYUV422((BYTE*)RGB,YBuffer,UVBuffer,capW,capH,capW,0);
	//size_compress=COMPRESS_FRAME(YBuffer, UVBuffer, UVBuffer+capW*capH/2, capW,capH,	// LOGRUS
	//		                     compressed, Mbps*BPS/*-size_audio*/, Mbps, 1);	

	size_compress=COMPRESS_FRAME(YBuffer, UVBuffer, UVBuffer+capW*capH/2, capW,capH, 
			                         compressed, LOGRUS_reqbitrate*BPS/*-size_audio*/, 1, CMemory);

#ifndef _VC10EA_
	if(cap_process) cap_process(compressed,size_compress, TRUE);
#else //ndef _VC10EA_
	Process(compressed, size_compress, TRUE);
#endif //ndef _VC10EA_
	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);
}

void VideoCompression_SetCompressedStillImage(void* compr,int size)
{	
	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
#ifndef _VC10EA_
	if(cap_process) cap_process(compr, size_compress, TRUE);
#else //ndef _VC10EA_
	Process(compr, size_compress, TRUE);
#endif //ndef _VC10EA_
	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);
}


void VideoCompression_CompressStillImage(void* RGB,void** dest, int* size)
{
	
	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	INTEL_RGBToYUV422((BYTE*)RGB,YBuffer,UVBuffer,capW,capH,capW,0);
	size_compress=COMPRESS_FRAME(YBuffer, UVBuffer, UVBuffer+capW*capH/2, capW,capH, 
			                         compressed, LOGRUS_reqbitrate*BPS/*-size_audio*/, 1, CMemory);
    *size = size_compress;
    *dest = compressed;
	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);
}

BOOL VideoCompression_Create()
{
	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	printf("Initializing Streambox PVR-LOGRUS codec. \n");

	//------------------- I_N_I_T___C_O_D_E_C ---------------------------  LOGRUS
	
	COMMON_INIT();
	CMemory = INIT_ENCODER(g_cs.w, g_cs.h);


	//-------------------------------------------------------------------

	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	return TRUE;
}

void VideoCompression_Destroy()
{
	EnterCriticalSection(&g_cs.CS_VIDEO_ENCODING);
	FREE_ENCODER(CMemory);
	LeaveCriticalSection(&g_cs.CS_VIDEO_ENCODING);
}